package baidu.guessnum;
//blog http://hi.baidu.com/wzwahl36
//author wzwahl36


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class NumGuessing
	implements ActionListener
{

	private JFrame frame;
	private JPanel centerJPanel;
	private JPanel southJPanel;
	private JTextArea showArea;
	private JTextField guessField;
	private JButton commitJButton;
	private Font font;
	private boolean isOver;
	private int guessTimeCount;
	private int guessingNum[];
	private String guessedNum[];
	
	
	public NumGuessing()
	{
		isOver = false;
		guessTimeCount = 0;
		guessedNum = new String[10];
		frame = new JFrame("NumGuessing");
		frame.setSize(200, 300);
		frame.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - frame.getSize().width) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - frame.getSize().height) / 2);
		font = new Font(null, 2, 15);
		centerJPanel = new JPanel();
		southJPanel = new JPanel();
		showArea = new JTextArea("������������ֵĽ��\n");
		showArea.setFont(font);
		showArea.setEnabled(false);
		guessField = new JTextField(6);
		guessField.setFocusable(true);
		guessField.setFont(font);
		commitJButton = new JButton("Enter");
		commitJButton.setFont(font);
		frame.setLayout(new BorderLayout());
		centerJPanel.setLayout(new GridLayout(1, 2));
		centerJPanel.add(showArea);
		southJPanel.add(guessField);
		southJPanel.add(commitJButton);
		frame.add(centerJPanel, "Center");
		frame.add(southJPanel, "South");
		frame.validate();
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(3);
		
		commitJButton.addActionListener(this);
/*		guessField.addKeyListener(new KeyAdapter() {
			
			public void keyTyped(KeyEvent e){
				actionPerformed(new ActionEvent(e.getSource(), e.getID(),""));
			}
		});
*/
		guessingNum = Methods.RandomNum();
	}

	public void actionPerformed(ActionEvent e) {
		String guessString = guessField.getText();
		if (!isOver && guessTimeCount < 10)
		{
			if (Methods.CheckIsFourNum(guessString))
			{
				if (!Methods.IsGuessed(guessString, guessedNum))
				{
					if (!Methods.HasSameNum(guessString))
					{
						showArea.append(guessString);
						showArea.append((new StringBuilder(String.valueOf(Methods.GetResult(guessingNum, guessString)))).append("\n").toString());
						guessField.setText(null);
						guessedNum[guessTimeCount] = guessString;
						guessTimeCount++;
						if (Methods.GetResult(guessingNum, guessString).equals("4A4B"))
						{
							isOver = true;
							JOptionPane.showMessageDialog(null, "You Are The Winner");
						}
						if (guessTimeCount >= 10)
						{
							isOver = true;
							JOptionPane.showMessageDialog(null, "��Ӧ������ʮ�Σ����ˣ�");
							guessField.setText(null);
						}
					} else
					{
						guessField.setText(null);
						JOptionPane.showMessageDialog(null, "���������������ͬ�����֣���������д��");
					}
				} else
				{
					guessField.setText(null);
					JOptionPane.showMessageDialog(null, "����������Ѿ��¹��ˣ�");
				}
			} else
			{
				guessField.setText(null);
				JOptionPane.showMessageDialog(null, "������һ����λ����");
			}
		} else
		{
			guessField.setEnabled(false);
		}
	}
}
